package DTO;

import java.io.ByteArrayInputStream;
import java.io.Serializable;
import java.util.ArrayList;

import org.Consts;
import org.Globals;
import org.Utility;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

public class FavouriteColorDTO extends BaseDTO implements Serializable {
	
	public int intUuid;
	public Bitmap image;
	public int colorValue;
	
	public FavouriteColorDTO(int id, SQLiteDatabase db) { 
		intUuid = id;
		
		Cursor c = null;
		if ("".equalsIgnoreCase(init_statement)) { 
				String sql="Select " + Consts.COLUMN_IMAGE + "," + Consts.COLUMN_COLOR_VALUE + " From " +Consts.TABLE_FAVOURITE_COLOR + " Where _id = " +intUuid + "";
				c=db.rawQuery(sql, null);
				/*c = db.query(Consts.TABLE_GRADIENTS, new String[]{Consts.COLUMN_IMAGE, Consts.COLUMN_HUE},
						"_id=?", new String[]{intUuid + ""}, null, null, null);*/

		}
		
		if (c != null) { 
			if (c.moveToFirst()) { 

				byte[] pic=(c.getBlob(0));   
				  ByteArrayInputStream imageStream = new ByteArrayInputStream(pic);
				  image= BitmapFactory.decodeStream(imageStream);
				  //img=Utility.byteArrayToDrawable(c.getBlob(0));

				  colorValue=c.getInt(1);
				
			} else { 
				Log.d("", "== something happened, could not extract from database");
			}
			if(c != null) {
				c.close();
			}
		} else { 
			Log.d("", "== c was null");
		}
	}
	
	public FavouriteColorDTO() {
		intUuid=-1;
		image=null;
		colorValue=0;
	}

	public boolean insertIntoDatabase(SQLiteDatabase db) {
		this.db=db;
		try {
			ContentValues values = new ContentValues();
			byte[] img=Utility.bitmaptoByteArray(image);
			values.put(Consts.COLUMN_IMAGE, img);
			values.put(Consts.COLUMN_COLOR_VALUE, colorValue);
			int id = (int) db.insert(Consts.TABLE_FAVOURITE_COLOR, null, values);

			if (id>0){
				intUuid=id;
				Log.d("", "== was successfully inserted in db, Object");
			}
			else
				Log.d("", "== unsuccessful in inserting in db, Object");

			return id>0;
		} catch (Exception e) {
			e.printStackTrace();

			Log.d("", "== unsucessful in inserting in db, Object, exception");
		}
		return false;
	}
	
public static ArrayList<FavouriteColorDTO> getObjectsInDatabase(SQLiteDatabase db) { 
		
		ArrayList<FavouriteColorDTO> objects = new ArrayList<FavouriteColorDTO>();
		
		try { 
			//SELECT id FROM drug_extra WHERE drug_id=?
			//sqlite3_bind_int(statement, 1, drugUUID)
			String sql = "SELECT _id FROM " + Consts.TABLE_FAVOURITE_COLOR;
			Cursor c = db.rawQuery(sql, null);
			
			while (c.moveToNext()) { 
				
				int primaryKey = c.getInt(0);
				FavouriteColorDTO obj = new FavouriteColorDTO(primaryKey, db);
				objects.add(obj);
			}
			
			if(c != null) {
				c.close();
			}
			
		} catch (Exception e) { 
			e.printStackTrace();
			Log.d("", "== something went wrong, drugExtra, getDrugExtraDB");
		}

		return objects;
	}

public void removeFromDB(SQLiteDatabase db) {
	try {
		String sql = "DELETE FROM "+Consts.TABLE_FAVOURITE_COLOR + " WHERE _id=?";
		// int x= db.rawQuery(sql, new String[] { intUuid + "" }); 
		int x=db.delete(Consts.TABLE_FAVOURITE_COLOR, "_id" + "=" + intUuid, null);
				//new String[] { intUuid + "" }) > 0;
		 boolean state=x>0;
		if (state)
			Log.d("", "== successfully deleted, dose");
		else
			Log.d("", "== problem deleting, dose");
	} catch (SQLException e) {
		Log.e("Database Error: ", e.toString());
		e.printStackTrace();
	}
}

}
